﻿#pragma once
#include "AutoReleaseRunable.h"

#include "BaseMsg.h"

#include "TLV.h"

#include <QMutex>

#include "commonreq.h"

#include "appupdatereq.h"

int sendMsgWithCommonResp(BaseMsg * msg, QString ip, int port, QString &failReason);


class TcpConnectHandler : public AutoReleaseRunable
{
public:
	TcpConnectHandler(SOCKET connection);

	virtual ~TcpConnectHandler();

	virtual int start();

    void handleAppUpdateReq(AppUpdateReq * req);

    void handleGetAppRunningStateCommonReq(CommonReq * req);

    void handleGetSysRunningStateCommonReq(CommonReq * req);


	static void initSocket();

	//socket 消息收发接口
	int readMsgType(SString &msgType);

	int readNextTlv(TLV *& tlv);

	int readMagNum();

	BaseMsg * readMsg();

	int sendMsg(BaseMsg * msg);



public:
	static TcpConnectHandler* connect(SString ip, int port);


public:
	

	SOCKET connection;

	bool connectionIsGood = true;

	static long connnectionCount;

    static QMutex *connectionCountLock;

    SString remoteIP;

    int remotePort;

};

